﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Server.Business;
using BySoft.Server.Common;
using BySoft.Server.Consts;
using BySoft.Web.Utility;

namespace BySoft.Web.Site.MasteData
{
    public partial class CustomerList : AuthBasePage
    {
        int m_count = 0;
        public int IPageIndex
        {
            get
            {
                if (null == Session["CustomerIPageIndex"])
                {
                    return 0;
                }
                else
                {
                    return (int)Session["CustomerIPageIndex"];
                }
            }
            set
            {
                Session["CustomerIPageIndex"] = value;
            }
        }


        public DataTable DTCustomerListData
        {
            get
            {
                if (null == Session["CustomerData"])
                {
                    return CreateQueryDT();
                }
                else
                {
                    return (DataTable)Session["CustomerData"];
                }
            }
            set
            {
                Session["CustomerData"] = value;
            }
        }
        private void GetQuery()
        {
            DataTable dt = DTCustomerListData.Copy();
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                txtCustomerID.Text = CommonMethod.GetString(row["CustomerID"]);
                txtCustomerName.Text = CommonMethod.GetString(row["CustomerName"]);
            }
        }

        private void SetQuery()
        {
            DataTable dt = CreateQueryDT();
            DataRow dr = dt.NewRow();
            dr["CustomerID"] = txtCustomerID.Text.Trim();
            dr["CustomerName"] = txtCustomerName.Text.Trim();

            dt.Rows.Add(dr);
            DTCustomerListData = dt;
        }

        private DataTable CreateQueryDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("CustomerID");
            dt.Columns.Add("CustomerName");
            return dt;
        }

        private void RemoveSession()
        {
            List<string> lst = GetSession();
            lst.Add("CustomerIPageIndex");
            lst.Add("CustomerListData");
            ClearSession(lst);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RemoveSession();
                InitData();
                GetQuery();
                BindDDL();
            }
        }

        private void BindDDL()
        {
            
        }


        private void InitData()
        {
            BindData(0);
        }
        /// <summary>
        /// 查询方法
        /// </summary>
        /// <param name="pageIndex">pageindex</param>
        private void BindData(int pageIndex)
        {
            //SetQuery();
            pageIndex = IPageIndex;
            string userid = "";
            if (!string.IsNullOrEmpty(CurrentUser.UserID) && CurrentUser.UserID != CommonConsts.ADMIN_USER)
            {
                userid = CurrentUser.UserID;
            }
            DataTable dt = MdCustomer.SelectList(txtCustomerID.Text.Trim(), txtCustomerName.Text.Trim(), userid, pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            if (dt.Rows.Count != 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string type = dt.Rows[i]["CustomerFrom"].ToString().Trim();
                    dt.Rows[i]["CustomerFrom"] = SysParameter.GetParameterName("CustomerFrom", type);
                    string scaler = dt.Rows[i]["CustomerScaler"].ToString().Trim();
                    dt.Rows[i]["CustomerScaler"] = SysParameter.GetParameterName("CustomerScaler", scaler);
                }
                grdResult.DataSource = dt;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        protected void pcData_PageIndexClick(object sender, Controls.PageIndexClickEventArgs e)
        {
            IPageIndex = e.PageIndex;
            BindData(e.PageIndex);
        }

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnDel");
                Button btnDel = (Button)e.Row.FindControl("btnDel");
                if (ibtn != null)
                {
                    ibtn.OnClientClick = "ShowTopConfirm('提示信息','" + Message.ConfirmDelete + "','confirmCallback(\\'" + btnDel.ClientID + "\\')');return false;";
                }
            }
        }

        protected void grdResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCode = e.CommandArgument.ToString();
            string[] strs = strCode.Split(',');
            switch (e.CommandName)
            {
                case "viewdata":
                    Response.Redirect(string.Format("CustomerView.aspx?id={0}", HttpUtility.UrlEncode(strCode)));
                    break;
                case "edi":
                    Response.Redirect(string.Format("CustomerEdit.aspx?id={0}", HttpUtility.UrlEncode(strCode)));
                    break;
                case "del":
                    MdCustomerdetail.Del(strCode);
                    MdCustomer.Del(strCode);
                    BindData(0);
                    UIHelper.AlertMessage(this.Page, Message.DeleteSuccess);
                    break;
                default:
                    break;
            }
        }
    }
}